package com.zhu.gmallpublisher.controller;


import com.zhu.gmallpublisher.bean.CategoryCommodityStats;
import com.zhu.gmallpublisher.bean.TradeMarkCommodityStats;
import com.zhu.gmallpublisher.bean.TradeMarkOrderAmountPieGraph;
import com.zhu.gmallpublisher.service.CommodityStatsService;
import com.zhu.gmallpublisher.utils.GetDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

/**
 * 商品主题控制层
 */
@RestController
@RequestMapping("zhu/gmall/sugar/commodity")
public class CommodityStatsController {

    @Autowired
    private CommodityStatsService commodityStatsService;


    /**
     * 商品主题品牌交易分析   用户 订单数量  退单 用户数量
     * @param selectDate
     * @return
     */
    @RequestMapping("/trademark")
    public String getTrademarkCommodityStats(@RequestParam(value = "date",defaultValue = "0") Integer selectDate){

        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<TradeMarkCommodityStats> tradeMarkCommodityStatsList= commodityStatsService.getTrademarkCommodityStatsService(selectDate);
        if(tradeMarkCommodityStatsList == null || tradeMarkCommodityStatsList.size() ==0 ){
            return  "";
        }
        StringBuilder dataJsonString = new StringBuilder("[");
        for (int i = 0; i < tradeMarkCommodityStatsList.size(); i++) {
            TradeMarkCommodityStats tradeMarkCommodityStats = tradeMarkCommodityStatsList.get(i);
            String tradeMarkName = tradeMarkCommodityStats.getTradeMarkName();
            Double orderAmount = tradeMarkCommodityStats.getOrderAmount();
            Integer orderCt = tradeMarkCommodityStats.getOrderCt();
            Integer refundCt = tradeMarkCommodityStats.getRefundCt();
            Integer refundUuCt = tradeMarkCommodityStats.getRefundUuCt();
            Integer uuCt = tradeMarkCommodityStats.getUuCt();

            dataJsonString.append("{\n" +
                    "\t\"trademark\": \"" + tradeMarkName + "\",\n" +
                    "\t\"order_count\": \"" + orderCt + "\",\n" +
                    "\t\"uu_count\": \"" + uuCt + "\",\n" +
                    "\t\"order_amount\": \"" + orderAmount + "\",\n" +
                    "\t\"refund_count\": \"" + refundCt + "\",\n" +
                    "\t\"refund_uu_count\": \"" + refundUuCt + "\"\n" +
                    "}");
            if(i < tradeMarkCommodityStatsList.size()-1 ){
                dataJsonString.append(",");
            }else {
                dataJsonString.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"品牌名称\",\n" +
                "        \"id\": \"trademark\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单数\",\n" +
                "        \"id\": \"order_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单人数\",\n" +
                "        \"id\": \"uu_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单金额\",\n" +
                "        \"id\": \"order_amount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"退单数\",\n" +
                "        \"id\": \"refund_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"退单人数\",\n" +
                "        \"id\": \"refund_uu_count\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + dataJsonString + "\n" +
                "  }\n" +
                "}";
    }


    /**
     * 商品主题  品牌交易分析  只统计品牌名称 和订单总金额
     * @param selectDate
     * @return
     */
    @RequestMapping("/tradeMarkPie")
    public String getTmOrderAmtPieGra(@RequestParam(value ="date",defaultValue = "0") Integer selectDate){

        if(selectDate == 0){
            selectDate = GetDateUtil.getCurrentTime();
        }
        List<TradeMarkOrderAmountPieGraph> tradeMarkList = commodityStatsService.getTmOrderAmtPieGra(selectDate);
        if (tradeMarkList == null || tradeMarkList.size() == 0 ){
            return "";
        }
        StringBuilder dataStringJson = new StringBuilder("[");
        for (int i = 0; i < tradeMarkList.size(); i++) {
            TradeMarkOrderAmountPieGraph tradeMarkOrderAmountPieGraph = tradeMarkList.get(i);
            Double orderAmount = tradeMarkOrderAmountPieGraph.getOrderAmount();
            String trademarkName = tradeMarkOrderAmountPieGraph.getTrademarkName();
            dataStringJson
                    .append("{\n" + "      \"name\": \"").append(trademarkName)
                    .append("\",\n").append("      \"value\": ")
                    .append(orderAmount).append("\n").append("    }");
            if(i < tradeMarkList.size() - 1){
                dataStringJson.append(",");
            }else {
                dataStringJson.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+ dataStringJson +"\n" +
                "}";

    }

    /**
     * 商品主题  品牌交易分析
     * @param selectData
     * @return
     */
    @RequestMapping("/category")
    public String getCategoryCommodityStats(@RequestParam(value = "data",defaultValue = "0") Integer selectData){
        if(selectData == 0){
            selectData = GetDateUtil.getCurrentTime();
        }
        List<CategoryCommodityStats> categoryCommodityStatsList = commodityStatsService.getCategoryCommodityStats(selectData);
        if(categoryCommodityStatsList == null || categoryCommodityStatsList.size() == 0){
            return "";
        }
        StringBuilder dataStringJson = new StringBuilder("[");
        for (int i = 0; i < categoryCommodityStatsList.size(); i++) {
            CategoryCommodityStats categoryCommodityStats = categoryCommodityStatsList.get(i);
            String category1Name = categoryCommodityStats.getCategory1_name();
            String category2Name = categoryCommodityStats.getCategory2_name();
            String category3Name = categoryCommodityStats.getCategory3_name();
            Integer orderCt = categoryCommodityStats.getOrderCount();
            Double orderAmount = categoryCommodityStats.getOrderAmount();
            Integer uuCt = categoryCommodityStats.getUuCount();
            Integer refundCt = categoryCommodityStats.getRefundCt();
            Double refundAmount = categoryCommodityStats.getRefundUuCt();

            dataStringJson.append("{\n" +
                    "\t\"category1_name\": \"" + category1Name + "\",\n" +
                    "\t\"category2_name\": \"" + category2Name + "\",\n" +
                    "\t\"category3_name\": \"" + category3Name + "\",\n" +
                    "\t\"order_count\": \"" + orderCt + "\",\n" +
                    "\t\"uu_count\": \"" + uuCt + "\",\n" +
                    "\t\"order_amount\": \"" + orderAmount + "\",\n" +
                    "\t\"refund_count\": \"" + refundCt + "\",\n" +
                    "\t\"refund_uu_count\": \"" + refundAmount + "\"\n" +
                    "}");
            if(i < categoryCommodityStatsList.size() - 1){
                dataStringJson.append(",");
            }else {
                dataStringJson.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"一级品类名称\",\n" +
                "        \"id\": \"category1_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"二级品类名称\",\n" +
                "        \"id\": \"category2_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"三级品类名称\",\n" +
                "        \"id\": \"category3_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单数\",\n" +
                "        \"id\": \"order_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单人数\",\n" +
                "        \"id\": \"uu_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单金额\",\n" +
                "        \"id\": \"order_amount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"退单数\",\n" +
                "        \"id\": \"refund_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"退单人数\",\n" +
                "        \"id\": \"refund_uu_count\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + dataStringJson + "\n" +
                "  }\n" +
                "}";


    }


}

